;(function ($){
  var foo = function (options){
    var self = this;
    self.options = $.extend({
      parent: $("#myForm"),
      regParent: $("#regForm"),
      register: $("[node-type=register]"),
      submit: $("[node-type=submit]")
    }, options);
    self.bindEvent();
  };

  foo.prototype = {
    bindEvent: function (){
      var self = this;
      self.login();
      self.register();
    },
    login: function (){
      var self = this,
          parent = self.options.parent,
          submit = self.options.submit;

      submit.on("click", function (){
        var jqXML = $.ajax({
          url: "/api/login",
          dataType: "json",
          type: "post",
          data: parent.serialize()
        });

        jqXML.done(function (json){
          if(json.code == 200){
            location.href = "/home";
          }else{
            alert(json.msg);
          }
        });
      });
    },
    register: function (){
      var self = this,
          regParent = self.options.regParent,
          register = self.options.register;

      register.on("click", function (){
        var jqXML = $.ajax({
          url: "/register",
          dataType: "json",
          type: "post",
          data: regParent.serialize()
        });

        jqXML.done(function (json){
          if(json.code == 200){
            alert(json.msg);
          }else{
            alert(json.msg);
          }
        });
      });
    }
  };

  new foo();
})(jQuery);
